 import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import {resolve} from 'path'
 import UnoCSS from 'unocss/vite'

import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'


// https://vite.dev/config/
export default defineConfig(({mode}) =>{
  const config = loadEnv(mode, './')
  const server = {
    port: 8080,
    open: true,
    proxy:{}
  }
  server.proxy[config.VITE_API_URL] = {
    target: config.VITE_BASE_URL,
    secure: false,
    changeOrigin: true,
    rewrite: (path) => path.replace(config.VITE_API_URL, "")
  }
  return {
    base: '/',
    css:{
      preprocessorOptions:{
        scss:{
          api:'modern-compiler'
        }
      }
    },
    plugins: [
      vue(),
      UnoCSS(),
      AutoImport({
        imports: ['vue', 'vue-router', 'pinia',"@vueuse/core"],
        resolvers: [ElementPlusResolver()],
        dts: "src/auto-imports.d.ts",
      }),
      Components({
        resolvers: [ElementPlusResolver()],
        dts: "src/components.d.ts",
      }),
    ],
    resolve: {
      alias: {
        '@v': resolve(__dirname, 'src/views'),
        '@c': resolve(__dirname, 'src/components'),
        '@cp': resolve(__dirname, 'src/composables'),
        '@u': resolve(__dirname, 'src/utils'),
        '@a': resolve(__dirname, 'src/assets'),
        '@api': resolve(__dirname, 'src/api'),
        '@r': resolve(__dirname, 'src/router'),
        '@s': resolve(__dirname, 'src/store'),
        '@': resolve(__dirname, 'src'),
      },
    },
    server,
  }
})

